<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="initial-scale=1, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0" />
    <title>仿QQ列表侧滑删除</title>
    <style>
        html,
        body {
            margin: 0;
            padding: 0;
            height: 100%;
            background: #dadada;
            -webkit-user-select:none;
            -moz-user-select:none;
            -o-user-select:none;
            user-select:none;
        }

        header {
            height: 44px;
            line-height: 44px;
            text-align: center;
            background: #da020c;
            color: #fff;
        }

        h1 {
            margin: 0;
            padding: 0;
            font-size: 18px;
            font-weight: normal;
        }

        header>a {
            height: 34px;
            line-height: 34px;
            position: absolute;
            top: 5px;
            left: 10px;
        }

        #wrapper {
            position: absolute;
            top: 44px;
            bottom: 0;
            width: 100%;
        }

        #scroller {
            margin: 0;
            padding: 0;
            background: #fff;
        }

        .item {
            width: 100%;
            height: 64px;
            border-bottom: 1px solid #ddd;
            line-height: 64px;
        }
        .item-scroller {
            width: 130%;
            /* 添加absolute属性消除安卓机上滑动删除按钮时的文字重影 */
            position: absolute;
        }
        .content {
            width: 77%;
            height: 100%;
            float: left;
            padding-left: 20px;
            box-sizing: border-box;
        }
        .del {
            width: 23%;
            height: 100%;
            float: right;
            background: red;
            color: #fff;
            text-align: center;
        }
    </style>
</head>

<body>
    <header>
        <h1>列表单例侧滑</h1>
        <a onclick="javascript:history.back()">&lt;返回</a>
    </header>
    <div id="wrapper">
        <div id="scroller">
            <!-- 用js添加内容 -->
        </div>
    </div>

    <script src="../vendor/fastclick.js"></script>
    <script src="../src/jroll.js"></script>
    <script>
        // 给scroller添加元素
        var count = 50
        var html = ''
        while (count--) {
            html += 
            '<div class="item">\
                <div class="item-scroller">\
                    <div class="content">列表内容</div>\
                    <div class="del">删除</div>\
                </div>\
            </div>'
        }
        document.getElementById('scroller').innerHTML = html

        // 创建外层jroll实例
        var jroll = new JRoll("#wrapper", {
            id: "demo9",scrollBarY:true
        });

        var items = document.querySelectorAll(".item");
        var current = null; //保存当前滑开的item

        for (var i=0,l=items.length; i<l; i++) {
            // 每行创建jroll实例
            var j = new JRoll(items[i], {
                scrollX:true,
                bounce:false
            });

            j.on("scrollStart", function() {
                if (current && current !== this) {
                    current.scrollTo(0, 0, 100);
                    current = null;
                }
            });

            j.on("scroll", function(e) {
                if (this.x === 0 && !current) {
                    this.call(jroll, e);
                } else {
                    current = this;
                }
            });

            j.on("scrollEnd", function() {
                if (this.x > -50) {
                    this.scrollTo(0, 0, 100);
                    current = null;
                } else {
                    this.scrollTo(this.maxScrollX, 0, 100);
                }
            })
        };

        //添加点击删除按钮的事件
        jroll.scroller.addEventListener("click", function(e) {
            if (e.target.className === "del") {
                alert("点击删除");
            }
        }, false);

        FastClick.attach(document.body);
    </script>
</body>

</html>